package ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.UiSettings;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.core.SuggestionCity;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.fy.fyzf.R;
import com.fy.fyzf.api.ApiConstant;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.base.Contans;
import com.fy.fyzf.bean.BaseData;
import com.fy.fyzf.bean.DemanddetailBean;
import com.fy.fyzf.bean.HousingDetailBean;
import com.fy.fyzf.bean.HousingFllowListBean;
import com.fy.fyzf.bean.SearchPassengerBean;
import com.fy.fyzf.dialog.HousingSharePopwindow;
import com.fy.fyzf.glideUtils.GlideUtils;
import com.fy.fyzf.presenter.HouseDetailPresenter;
import com.fy.fyzf.statusbar.Eyes;
import com.fy.fyzf.utils.AppUtils;
import com.fy.fyzf.utils.Base64Utils;
import com.fy.fyzf.utils.DensityUtil;
import com.fy.fyzf.utils.MathUtils;
import com.fy.fyzf.utils.SPUtils;
import com.fy.fyzf.utils.ShareHelper;
import com.fy.fyzf.view.HouseDetailCallBack;
import com.fy.fyzf.view.SpacesItemDecoration;
import com.fy.fyzf.weight.CommonDialog;
import com.fy.fyzf.weight.PoiOverlay;
import com.google.gson.Gson;
import com.mob.MobSDK;
import com.stx.xhb.androidx.XBanner;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.litepal.util.Const;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cc.shinichi.library.ImagePreview;
import cc.shinichi.library.bean.ImageInfo;
import cc.shinichi.library.view.listener.OnBigImageClickListener;
import cc.shinichi.library.view.listener.OnBigImageLongClickListener;
import cc.shinichi.library.view.listener.OnBigImagePageChangeListener;
import cc.shinichi.library.view.listener.OnDownloadClickListener;
import cc.shinichi.library.view.listener.OnOriginProgressListener;
import ui.adapter.HousingFllowListAdapter;

/**
 * 房源详情
 */
public class HouseDetailActivity extends BaseActivity<HouseDetailPresenter> implements HouseDetailCallBack, PoiSearch.OnPoiSearchListener {

    @BindView(R.id.xbanner)
    XBanner xBanner;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.leftView)
    TextView leftView;
    @BindView(R.id.tv_center_title)
    TextView tvCenterTitle;
    @BindView(R.id.right)
    TextView right;
    @BindView(R.id.titleBar)
    RelativeLayout titleBar;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.titleLine)
    View titleLine;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_tag)
    TextView tvTag;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_Area)
    TextView tvArea;
    @BindView(R.id.tv_fitment)
    TextView tvFitment;
    @BindView(R.id.tv_house_name)
    TextView tvHouseName;
    @BindView(R.id.tv_direction)
    TextView tvDirection;
    @BindView(R.id.tv_room_number)
    TextView tvRoomNumber;
    @BindView(R.id.tv_subway)
    TextView tvSubway;
    @BindView(R.id.tv_fitment_status)
    TextView tvFitmentStatus;
    @BindView(R.id.tv_purpose)
    TextView tvPurpose;
    @BindView(R.id.tv_add_staff)
    TextView tvAddStaff;
    @BindView(R.id.tv_price_month)
    TextView tvPriceMonth;
    @BindView(R.id.tv_commission)
    TextView tvCommission;
    @BindView(R.id.tv_look_type)
    TextView tvLookType;
    @BindView(R.id.tv_rent_fee_time)
    TextView tvRentFeeTime;
    @BindView(R.id.tv_pay_type)
    TextView tvPayType;
    @BindView(R.id.tv_progressive)
    TextView tvProgressive;
    @BindView(R.id.tv_in_pose)
    TextView tvInPose;
    @BindView(R.id.tv_entrust_time)
    TextView tvEntrustTime;
    @BindView(R.id.tv_quality_good)
    TextView tvGoofQuality;
    @BindView(R.id.tv_location)
    TextView tvLocation;
    @BindView(R.id.rightImage)
    ImageView rightImageView;
    @BindView(R.id.mapView)
    MapView mapView;
    @BindView(R.id.tv_look_owner)
    TextView tvLookOwner;
    @BindView(R.id.edit_remark)
    TextView editRemark;
    @BindView(R.id.tv_save)
    TextView tvSave;
    @BindView(R.id.tv_look)
    TextView tvDaiKan;
    @BindView(R.id.iv_logo)
    ImageView ivLogo;
    @BindView(R.id.tv_demand_name)
    TextView tvDemandName;
    @BindView(R.id.ll_demand)
    LinearLayout llDemand;
    @BindView(R.id.ll_price)
    LinearLayout llPrcie;
    @BindView(R.id.ll_fllow)
    LinearLayout llFllow;
    @BindView(R.id.recycleView_Fllow)
    RecyclerView recycleViewFllow;
    @BindView(R.id.tv_sell_type)
    TextView tvSelleType;

    private int type;
    //    private List<HousingDetailBean.HousingImagesBean> housingImages;
    private SearchPassengerBean searchPassengerBean = new SearchPassengerBean();
    private String collection;
    private int customerId;
    private HousingFllowListAdapter housingFllowListAdapter;

    @Override
    protected HouseDetailPresenter createPresenter() {
        return new HouseDetailPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_house_detail;
    }

    @Override
    public void initView() {
        super.initView();
        setTitleName(getString(R.string.housing_detail));
        Eyes.setAndroidNativeLightStatusBar(this, true);

        housingFllowListAdapter = new HousingFllowListAdapter();
        recycleViewFllow.setLayoutManager(new LinearLayoutManager(this));
        recycleViewFllow.setAdapter(housingFllowListAdapter);
        recycleViewFllow.addItemDecoration(new SpacesItemDecoration(DensityUtil.px2dip(this, 10)));
    }

    private Integer housingId;
    private Integer housingStatus;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void housingEditSuccess(String s) {
        if (s.equals("housingEditSuccess")) {
            initData();
        } else if (s.equals("editSuccess")) {
            mPresenter.getHousingFllow(housingId);
        }else if(s.equals("deleteHousing")){
            finish();
        }
    }

    @Override
    public void initData() {
        super.initData();
        registerEventBus(this);
        type = getIntent().getIntExtra("type", 0);
        collection = getIntent().getStringExtra("collection");
        customerId = getIntent().getIntExtra("customerId", -1);
        housingId = getIntent().getIntExtra("housingid", 0);
        tvDaiKan.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.daikan), null, null);
        if (collection == null) {
            tvSave.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.star_gray), null, null);
        } else {
            tvSave.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.icon_star), null, null);
        }

        rightImageView.setVisibility(View.VISIBLE);
        rightImageView.setBackgroundResource(R.mipmap.icon_more);
        mPresenter.getHousingDetail(housingId);
        if (type == 1 || type == 5) { //从首页进入 这两个按钮不显示
            tvSave.setVisibility(View.INVISIBLE);
            tvDaiKan.setVisibility(View.INVISIBLE);
        } else if (type == 3) { //推荐进入
            DemanddetailBean.PublisherDetailsReleaseVoBean.ListBeanX data = (DemanddetailBean.PublisherDetailsReleaseVoBean.ListBeanX) getIntent().getSerializableExtra("data");
            llDemand.setVisibility(View.VISIBLE);
            GlideUtils.loadCircleImage(data.getUserPortrait(), ivLogo);
            tvDemandName.setText(data.getUserName());
            tvSave.setVisibility(View.INVISIBLE);
            tvDaiKan.setVisibility(View.INVISIBLE);
            llFllow.setVisibility(View.GONE);
            llPrcie.setVisibility(View.GONE);
        } else if (type == 4) {
            llDemand.setVisibility(View.VISIBLE);
            GlideUtils.loadCircleImage(getIntent().getStringExtra("logo"), ivLogo);
            tvDemandName.setText(getIntent().getStringExtra("name"));
            llFllow.setVisibility(View.GONE);
            llPrcie.setVisibility(View.GONE);
            tvSave.setVisibility(View.INVISIBLE);
            tvDaiKan.setVisibility(View.INVISIBLE);
        }else {
            tvSave.setVisibility(View.VISIBLE);
            tvDaiKan.setVisibility(View.VISIBLE);
        }
    }

    PoiSearch.Query query;
    AMap map;

    private void initMap(String buildName) {
        map = mapView.getMap();
        tvLocation.setText(buildName);
        UiSettings uiSettings = map.getUiSettings();
        uiSettings.setMyLocationButtonEnabled(false);
        uiSettings.setScrollGesturesEnabled(false);
        uiSettings.setZoomControlsEnabled(false);
        map.setMyLocationEnabled(false);
//        query = new PoiSearch.Query(buildName, "", "杭州");// 第一个参数表示搜索字符串，第二个参数表示poi搜索类型，第三个参数表示poi搜索区域（空字符串代表全国）
        query = new PoiSearch.Query("", "120201", "杭州");// 第一个参数表示搜索字符串，第二个参数表示poi搜索类型，第三个参数表示poi搜索区域（空字符串代表全国）
        query.setPageSize(1000);// 设置每页最多返回多少条poiitem
        query.setPageNum(1);// 设置查第一页
        query.setCityLimit(true);
        PoiSearch poiSearch = new PoiSearch(this, query);
        poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(Contans.latitude,
                Contans.longitude), 10000));
        poiSearch.setOnPoiSearchListener(this);
        poiSearch.searchPOIAsyn();

        map.setOnMapClickListener(new AMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                Intent intent = new Intent(HouseDetailActivity.this, MapDetailActivity.class);
                intent.putExtra("name", tvLocation.getText().toString());
                startActivity(intent);
            }
        });
    }

    private HousingDetailBean housingData;

    @Override
    public void getHousingDetailSuccess(HousingDetailBean housingDetailBean) {
        housingData = housingDetailBean;
        housingStatus = housingDetailBean.getHousingStatus();
        initMap(housingDetailBean.getFloorName());
        showBanner(housingDetailBean);
        showData(housingDetailBean);
    }

    private void showData(HousingDetailBean housingDetailBean) {
        String s = housingDetailBean.getBuilding() == null ? "" : housingDetailBean.getBuilding();
        tvName.setText(housingDetailBean.getFloorName()+"  "+s );
        if (housingDetailBean.getBigOwner() == 0) {
            tvTag.setVisibility(View.GONE);
        } else {
            tvTag.setVisibility(View.VISIBLE);
            tvTag.setText("大业主");
        }
        if (housingDetailBean.getQualityHouse() == 0) {
            tvGoofQuality.setVisibility(View.GONE);
        } else {
            tvGoofQuality.setVisibility(View.VISIBLE);
            tvGoofQuality.setText("优质房源");
        }

        String purPose = "";
        StringBuilder stringBuilder = new StringBuilder();
        if (housingDetailBean.getHousingPurpose() == 0) {
            stringBuilder.append(MathUtils.rvZeroAndDot(housingDetailBean.getRentPrice())).append("元/m²/天");
            tvPrice.setText(showColorText(stringBuilder.toString(), MathUtils.rvZeroAndDot(housingDetailBean.getRentPrice()).length(), "#DF2627"));
            purPose = "出租";
            tvSelleType.setText("租");
        } else if (housingDetailBean.getHousingPurpose() == 1) {
            stringBuilder.append(MathUtils.rvZeroAndDot(housingDetailBean.getSellPrice())).append("元/m²");
            tvPrice.setText(showColorText(stringBuilder.toString(), MathUtils.rvZeroAndDot(housingDetailBean.getSellPrice()).length(), "#DF2627"));
            tvSelleType.setText("售");
            purPose = "出售";
        } else {
            stringBuilder.append(MathUtils.rvZeroAndDot(housingDetailBean.getRentPrice())).append("元/m²/天");
            tvPrice.setText(showColorText(stringBuilder.toString(), MathUtils.rvZeroAndDot(housingDetailBean.getRentPrice()).length(), "#DF2627"));
            purPose = "可租可售";
            tvSelleType.setText("可租可售");
        }


        StringBuilder stringBuilder1 = new StringBuilder();
        stringBuilder1.append(MathUtils.rvZeroAndDot(housingDetailBean.getHousingArea())).append("m²");
        tvPrice.setText(showColorText(stringBuilder.toString(), MathUtils.rvZeroAndDot(housingDetailBean.getRentPrice()).length(), "#DF2627"));
        tvArea.setText(showColorText(stringBuilder1.toString(), MathUtils.rvZeroAndDot(housingDetailBean.getHousingArea()).length(), "#DF2627"));
        //简装/精装/豪装
        switch (housingDetailBean.getRenovationType()) {
            case 1:
                tvFitment.setText("简装");
                break;
            case 2:
                tvFitment.setText("精装");
                break;
            case 3:
                tvFitment.setText("豪装");
                break;
            case 4:
                tvFitment.setText("无装修");
                break;
        }
        tvHouseName.setText(showColorText(new StringBuilder().append("楼盘: ").append(housingDetailBean.getFloorName()).toString(), 3, "#B9B9BA"));
        String dong = housingDetailBean.getBuilding() == null ? "独栋" : housingDetailBean.getBuilding() ;
        tvDirection.setText(showColorText(new StringBuilder().append("栋座: ").append(dong).toString(), 3, "#B9B9BA"));
        tvRoomNumber.setText(showColorText(new StringBuilder().append("房间号: ").append(housingDetailBean.getHousingNo()).toString(), 4, "#B9B9BA"));
        tvSubway.setText(showColorText(new StringBuilder().append("地铁: ").append(housingDetailBean.getSubwayName()).toString(), 3, "#B9B9BA"));
        tvFitmentStatus.setText(showColorText(new StringBuilder().append("装修情况: ").append(tvFitment.getText().toString()).toString(), 5, "#B9B9BA"));



        tvPurpose.setText(showColorText(new StringBuilder().append("用途: ").append(purPose).toString(), 3, "#B9B9BA"));
        tvAddStaff.setText(showColorText(new StringBuilder().append("添加员工: ").append(housingDetailBean.getUserName()).toString(), 5, "#B9B9BA"));
        tvPriceMonth.setText(showColorText(new StringBuilder().append("价格: ").append(tvPrice.getText()).toString(), 3, "#B9B9BA"));
        tvCommission.setText(showColorText(new StringBuilder().append("佣金: ").append(housingDetailBean.getFollowCommission()).toString(), 3, "#B9B9BA"));
        tvLookType.setText(showColorText(new StringBuilder().append("看房方式: ").append(housingDetailBean.getFollowWay()).toString(), 5, "#B9B9BA"));
        tvRentFeeTime.setText(showColorText(new StringBuilder().append("装修免租期: ").append(housingDetailBean.getDecorateFreetime()).toString(), 6, "#B9B9BA"));
        tvPayType.setText(showColorText(new StringBuilder().append("付款方式: ").append(housingDetailBean.getSigningYear()).toString(), 5, "#B9B9BA"));
        tvProgressive.setText(showColorText(new StringBuilder().append("递增及年限: ").append(housingDetailBean.getFollowIncreasing()).toString(), 6, "#B9B9BA"));
        if (housingDetailBean.getFollowSpecial() == null || housingDetailBean.getFollowSpecial().equals("")) {
            tvInPose.setText(showColorText(new StringBuilder().append("特殊限制: ").append("无").toString(), 5, "#B9B9BA"));
        } else {
            tvInPose.setText(showColorText(new StringBuilder().append("特殊限制: ").append(housingDetailBean.getFollowSpecial()).toString(), 5, "#B9B9BA"));
        }
        tvEntrustTime.setText(showColorText(new StringBuilder().append("委托日: ").append(housingDetailBean.getCreateTime()).toString(), 4, "#B9B9BA"));

        mPresenter.getHousingFllow(housingId);
    }

    private void showBanner(HousingDetailBean housingDetailBean) {
        List<String> bannerList = new ArrayList<>();
        List<HousingDetailBean.HousingImagesBean> housingImages = housingDetailBean.getHousingImages();
        if (housingImages.size() == 0) { //图片数据没有的时候 添加一个默认网络图片
            bannerList.add(Contans.DEFAULT_IMG_TAKE_PHOTO);
        } else {
            for (int i = 0; i < housingImages.size(); i++) {
                bannerList.add(housingImages.get(i).getHousingImage());
            }
        }
        xBanner.setData(bannerList, null);
        xBanner.setPointsIsVisible(false);
        xBanner.loadImage(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object model, View view, int position) {
                GlideUtils.loadRoundedCornersImage(bannerList.get(position), (ImageView) view, 1);
            }
        });
        final List<ImageInfo> imageInfoList = new ArrayList<>();
        ImageInfo imageInfo;
        for (int i = 0; i < housingImages.size(); i++) {
            imageInfo = new ImageInfo();
            imageInfo.setOriginUrl(housingImages.get(i).getHousingImage());
            imageInfo.setRemark(housingImages.get(i).getCompanyName() + "-" + housingImages.get(i).getUserName() + "上传时间" + housingImages.get(i).getCreateTime());
            imageInfo.setThumbnailUrl(housingImages.get(i).getHousingImage());
            imageInfoList.add(imageInfo);
        }
        xBanner.setOnItemClickListener(new XBanner.OnItemClickListener() {
            @Override
            public void onItemClick(XBanner banner, Object model, View view, int position) {
                // 完全自定义配置
                if (housingImages.size() ==0) {
                    Intent intent = new Intent(HouseDetailActivity.this, EditHousingActivity.class);
                    intent.putExtra("id", housingId);
                    startActivity(intent);
                   return;
                }
                ImagePreview.getInstance()
                        .setContext(HouseDetailActivity.this)
                        .setIndex(position)
                        .setImageInfoList(imageInfoList)
                        .setLoadStrategy(ImagePreview.LoadStrategy.Default)
                        .setFolderName("BigImageView")
                        .setZoomTransitionDuration(300)

                        .setShowErrorToast(false)
                        .setEnableClickClose(true)
                        .setEnableDragClose(true)
                        .setEnableUpDragClose(false)
                        .setShowCloseButton(false)
                        .setCloseIconResId(R.drawable.ic_action_close)
                        .setShowDownButton(true)
                        .setDownIconResId(R.drawable.icon_download_new)
                        .setShowIndicator(true)
                        .setIndicatorShapeResId(R.drawable.shape_indicator_bg)
                        .setErrorPlaceHolder(R.drawable.load_failed)
                        .setBigImageClickListener(new OnBigImageClickListener() {
                            @Override
                            public void onClick(Activity activity, View view, int position) {
                                // ...
                            }
                        })
                        .setBigImageLongClickListener(new OnBigImageLongClickListener() {
                            @Override
                            public boolean onLongClick(Activity activity, View view, int position) {
                                // ...
                                return false;
                            }
                        })
                        .setBigImagePageChangeListener(new OnBigImagePageChangeListener() {
                            @Override
                            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                            }

                            @Override
                            public void onPageSelected(int position) {
                            }

                            @Override
                            public void onPageScrollStateChanged(int state) {
                            }
                        })
                        .setDownloadClickListener(new OnDownloadClickListener() {
                            @Override
                            public void onClick(Activity activity, View view, int position) {
                                // 可以在此处执行您自己的下载逻辑、埋点统计等信息
                            }

                            @Override
                            public boolean isInterceptDownload() {
                                return false;
                            }
                        })
                        .setProgressLayoutId(ImagePreview.PROGRESS_THEME_CIRCLE_TEXT, new OnOriginProgressListener() {
                            @Override
                            public void progress(View parentView, int progress) {

                                // 需要找到进度控件并设置百分比，回调中的parentView即传入的布局的根View，可通过parentView找到控件：
                                ProgressBar progressBar = parentView.findViewById(R.id.sh_progress_view);
                                TextView textView = parentView.findViewById(R.id.sh_progress_text);
                                progressBar.setProgress(progress);
                                String progressText = progress + "%";
                                textView.setText(progressText);
                            }

                            @Override
                            public void finish(View parentView) {
                            }
                        })
                        .start();
            }
        });
        xBanner.setCustomPageTransformer(null);
    }

    @Override
    public void onError() {

    }

    @Override
    public void collectHousingSuccess(BaseData baseData, int i) {
        AppUtils.showToast("收藏成功");
        EventBus.getDefault().post("collectChange");
    }

    @Override
    public void removecollectHousingSuccess(BaseData baseData, int i) {
        AppUtils.showToast("取消成功");
        EventBus.getDefault().post("collectChange");
    }

    @Override
    public void editFollowSuccess(BaseData baseData) {

    }

    @Override
    public void getHousingFllowListSuccess(HousingFllowListBean housingFllowListBean) {
        housingFllowListAdapter.setNewData(housingFllowListBean.getList());
        if (housingFllowListBean.getList().size() <= 0) {
            llFllow.setVisibility(View.GONE);
        }
    }
    /**
     * 一个textView 展示两种颜色
     *
     * @param s
     * @param start
     */
    private SpannableStringBuilder showColorText(String s, int start, String color) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(s);
        spannableStringBuilder.setSpan(new ForegroundColorSpan(Color.parseColor(color)), 0, start, spannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), start, s.length(), spannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableStringBuilder;
    }
    private PoiResult poiResult; // poi返回的结果

    @Override
    public void onPoiSearched(PoiResult result, int rCode) {
        if (rCode == AMapException.CODE_AMAP_SUCCESS) {
            if (result != null && result.getQuery() != null) {// 搜索poi的结果
                if (result.getQuery().equals(query)) {// 是否是同一条
                    poiResult = result;
                    // 取得搜索到的poiitems有多少页
                    List<PoiItem> poiItems = poiResult.getPois();// 取得第一页的poiitem数据，页数从数字0开始
                    Log.e("数据", new Gson().toJson(poiItems));
                    List<SuggestionCity> suggestionCities = poiResult
                            .getSearchSuggestionCitys();// 当搜索不到poiitem数据时，会返回含有搜索关键字的城市信息
                    if (poiItems != null && poiItems.size() > 0) {
                        map.clear();// 清理之前的图标
                        PoiOverlay poiOverlay = new PoiOverlay(map, poiItems);
                        poiOverlay.removeFromMap();
                        poiOverlay.addToMap();
                        poiOverlay.zoomToSpan();
                    }
                }
            } else {

            }
        }
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {

    }


    @OnClick({R.id.tv_save, R.id.rightImage, R.id.tv_look_owner, R.id.edit_remark})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_save:
                CommonDialog commonDialog = new CommonDialog(HouseDetailActivity.this);
                if (collection == null) { //  未收藏
                    commonDialog.setContent("是否收藏该房源？");
                } else {
                    commonDialog.setContent("是否取消收藏该房源？");
                }
                commonDialog.toggleDialog();

                showCollect(commonDialog);
                break;
            case R.id.rightImage:
                initPop();
                break;
            case R.id.tv_look_owner:
                Intent intent = new Intent(this, OwnerListActivity.class);
                intent.putExtra("housingId", housingId);
                intent.putExtra("housingStatus", housingStatus);
                startActivity(intent);
                break;
            case R.id.edit_remark:
                intent = new Intent(this, EditFllowActivity.class);
                intent.putExtra("housingId", housingId);
                intent.putExtra("housingStatus", housingStatus);
                startActivity(intent);
                break;
        }
    }

    private void showCollect(CommonDialog dialog) {
        dialog.setOnconirmListener(new CommonDialog.OnConfirmClickListener() {
            @Override
            public void onClick() {
                searchPassengerBean.setHousingId(housingId);
                searchPassengerBean.setCustomerId(customerId);
                if (collection == null) {
                    mPresenter.addCollectHousing(searchPassengerBean, 0);
                } else {
                    mPresenter.removeCollectionHousing(searchPassengerBean, 0);
                }
            }
        });
    }

    private void initPop() {
        String userId = SPUtils.getInstance(this).getString("userId", "");
        MobSDK.submitPolicyGrantResult(true, null);
        HousingSharePopwindow housingSharePopwindow = new HousingSharePopwindow(this);
        housingSharePopwindow.show(rightImageView);
        housingSharePopwindow.getView(R.id.tv_share).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = ApiConstant.BASE_SERVER_URL+"/share/housing?housingId="+Base64Utils.encode(String.valueOf(housingId))+"&userId="+Base64Utils.encode(String.valueOf(userId));
                housingSharePopwindow.onDismiss();
                ShareHelper shareHelper = new ShareHelper(HouseDetailActivity.this);
                shareHelper.shareWx(housingData.getHousingImages().get(0).getHousingImage(),url);
            }
        });

        housingSharePopwindow.getView(R.id.tv_share_pyq).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = ApiConstant.BASE_SERVER_URL + "shareHousing/getHousing?housingId="+housingId;
                housingSharePopwindow.onDismiss();
                ShareHelper shareHelper = new ShareHelper(HouseDetailActivity.this);
                shareHelper.shareWechatMoments(housingData.getHousingImages().get(0).getHousingImage(), url);
            }
        });

        housingSharePopwindow.getView(R.id.tv_edit_housing).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                housingSharePopwindow.onDismiss();
                Intent intent = new Intent(HouseDetailActivity.this, EditHousingActivity.class);
                intent.putExtra("type", type);
                intent.putExtra("id", housingId);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterEventBus(this);
    }

}